<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\game\controller;

use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Description of CenterDataController
 *
 * @author Administrator
 */
class CenterDataController extends AdminBaseController {

    //列表
    public function index() {
        $pdata = \think\Db::name('data_center')->paginate(20);
        $page = $pdata->render();
        $items = $pdata->items();
        $this->assign('page', $page);
        $this->assign('items', $items);
        return $this->fetch();
    }

    public function edit() {
        $id = $this->request->param('id', 0);
        if ($id > 0) {
            $data = \think\Db::name('data_center')->find($id);
            $this->assign($data);
        }else{
            $this->assign('type_n',-1);
        }
        return $this->fetch();
    }

    public function editPost() {
        $id = $this->request->post('id', 0);
        $post['key_m'] = $this->request->post('key_m');
        $post['name_m'] = $this->request->post('name_m');
        $post['type_n'] = $this->request->post('type_n');
        $post['desc'] = $this->request->post('desc');
        $value = $this->request->post('value_m',null,null);
        $value = json_decode($value);
        if(empty($value)){
            $this->error('json 格式错误');
        }
        $post['value_m'] = json_encode($value);
        if ($id > 0) {
            $post['id'] = $id;
            Db::name('data_center')->update($post);
        } else {
            Db::name('data_center')->insert($post);
        }
        $this->success('保存成功');
    }

    public function delete() {
        $id = $this->request->param('id', 0);
        if ($id > 0) {
            Db::name('data_center')->delete($id);
        }
        $this->success('删除成功');
    }

    public function sync() {
        $ip = $this->request->post('url');
        $ids = $this->request->post('ids');
        $server = Db::name('server')->where('ip_s',$ip)->find();
        if(empty($server)){
            $url = url('calc/getserverid',['ip'=>$ip],true,true);//$this->request->scheme().'://'.$this->request->host().'/game/getserverid?ip='.$ip;
            $svid = \yxixia\NetUtil::getCurl($url);
            $server = Db::name('server')->find($svid);
        }
        if(empty($server)){
            return false;
        }
        $api = url('DataCenter/getData',[],true,true);
        $rs = \yxixia\NetUtil::serverSettingRequest('sync',$server, ['api'=>$api,'ids'=>$ids]);
        $this->success('同步中,请等待几秒钟',null,$rs);
    }
}
